package fightplan.core;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FPExceptionHandler {

	private static FPExceptionHandler instance;

	private JFrame parent;

	private FPExceptionHandler() {
	}

	public static FPExceptionHandler instance() {
		if (instance == null) {
			instance = new FPExceptionHandler();
		}
		return instance;
	}

	public void setParent(JFrame parent) {
		this.parent = parent;
	}

	public void handle(Throwable t) {
		String message = t.getCause().getMessage();
		if (t.getCause() instanceof NumberFormatException) {
			message = "Bitte eine gültige Zahl eingeben.";
		} else if (!(t.getCause() instanceof IllegalArgumentException)) {
			t.printStackTrace();
		}
		showWarning(message, "Fehler");
	}

	public void showWarning(String message, String title) {
		JOptionPane.showMessageDialog(parent, message, title, JOptionPane.WARNING_MESSAGE);
	}

	public void showInformation(String message, String title) {
		JOptionPane.showMessageDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE);
	}

}